using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IWS.Lib.SingleSignOnServer;
using System.Xml;

public partial class Admin_Controls_ServiceManagement : IWS.Lib.Utilities.Web.FormControl
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			this.Initialize();
		}
	}

	protected void cmdDownload_Click(object sender, EventArgs e)
	{
		LinkButton cmd = sender as LinkButton;
		if (cmd != null)
		{
			string key = HttpUtility.HtmlDecode(cmd.CommandArgument);
			XmlDocument doc = new XmlDocument();
			doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
			XmlElement root = doc.CreateElement("KeyFile");
			doc.AppendChild(root);
			XmlElement ekey = doc.CreateElement("Key");
			XmlCDataSection keysection = doc.CreateCDataSection(key);
			root.AppendChild(ekey);
			ekey.AppendChild(keysection);

			Response.Clear();
			Response.ContentType = "text/xml";
			Response.AddHeader("content-disposition", "attachment; filename=\"sso.xkey\"");
			Response.Write(doc.InnerXml);
			Response.End();
		}
	}
	protected void cmdDelete_Click(object sender, EventArgs e)
	{
		LinkButton cmd = sender as LinkButton;
		if (cmd != null)
		{
			Service.Destroy(HttpUtility.HtmlDecode(cmd.CommandArgument));
		}
		this.RefreshData();
	}

	protected override void InitializeCustom()
	{
		this.pnlNew.Visible = false;
		this.RefreshData();
	}

	protected override void OnSaveClick(object sender, EventArgs e)
	{
		try
		{
			if (this.pnlNew.Visible)
			{
				//make sure it doesn't already exist.
				Service svc = Service.FetchByID(System.Web.HttpUtility.HtmlDecode(lblID.Text));
				if (svc == null)
				{
					svc = new Service();
					svc.Id = System.Web.HttpUtility.HtmlDecode(lblID.Text);
				}

				DateTime expiration;
				if (DateTime.TryParse(txtExpiration.Text, out expiration))
				{
					svc.ExpirationDate = expiration;
				}
				svc.CreateDate = DateTime.UtcNow;
				svc.Save();
				ResetNewForm();

				this.pnlNew.Visible = false;
				this.RefreshData();
			}
			else
			{
				this.MessageBox1.MessageType = IWS.Lib.Utilities.Web.MessageBox.Type.Normal;
				this.MessageBox1.ShowMessage("Saved all settings successfully.");


				base.OnSaveClick(sender, e);
			}
		}
		catch (Exception ex)
		{
			this.LogException(ex, System.Reflection.Assembly.GetExecutingAssembly());
			MessageBox1.ShowMessage(ex.Message);
		}
	}

	protected override void OnBackClick(object sender, EventArgs e)
	{
		if (this.pnlNew.Visible)
		{
			this.pnlNew.Visible = false;
			this.ResetNewForm();
		}
		else
		{
			base.OnBackClick(sender, e);
		}
	}
	private void ResetNewForm()
	{
		this.lblID.Text = "";
		this.txtExpiration.Text = "";
	}
	private void RefreshData()
	{
		this.grdList.DataSource = new IWS.Lib.SingleSignOnServer.ServiceCollection().OrderByAsc("ExpirationDate").Load();
		this.grdList.DataBind();
	}
	protected void cmdNew_Click(object sender, EventArgs e)
	{
		pnlNew.Visible = true;
		lblID.Text = System.Web.HttpUtility.HtmlEncode(IWS.Lib.SingleSignOnServer.Utility.GetRandomUnicodeString(50));
		txtExpiration.Text = DateTime.Now.AddYears(1).ToUniversalTime().ToShortDateString();
	}
}
